package java12;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class TextFileStudent {
	public static void main(String[] args) {
		Student[] staff = new Student[3];
		staff[0] = new Student(1, "张三", 19, 65);
		staff[1] = new Student(2, "李四", 19, 75);
		staff[2] = new Student(3, "王五", 20, 85);
		try {
			PrintWriter out = new PrintWriter("d:/student.dat");
			writeData(staff, out);
			out.close();

			Scanner in = new Scanner(new FileReader("d:/student.dat"));
			Student[] newStaff = readData(in);
			in.close();

			for (Student e : newStaff)
				System.out.println(e);
		} catch (IOException exception) {
			exception.printStackTrace();
		}
	}

	private static void writeData(Student[] staff, PrintWriter out) throws IOException {
		out.println(staff.length);

		for (Student e : staff)
			e.writeData(out);
	}

	private static Student[] readData(Scanner in) {
		int n = in.nextInt();
		in.nextLine();

		Student[] students = new Student[n];
		for (int i = 0; i < n; i++) {
			students[i] = new Student();
			students[i].readData(in);
		}
		return students;
	}
}

class Student {
	private int id;
	private String name;
	private int age;
	private double grade;

	public Student() {

	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
	}
	
//201521123094
	public void writeData(PrintWriter out) {
		out.println(id + "|" + name + "|" + age + "|" + grade);

	}

	public void readData(Scanner in) {
		String line = in.nextLine();
		String[] tokens = line.split("\\|");
		id = Integer.parseInt(tokens[0]);
		name = tokens[1];
		age = Integer.parseInt(tokens[2]);
		grade = Double.parseDouble(tokens[3]);
	}

	public Student(int id, String name, int age, double grade) {
		this.id = id;
		this.setName(name);
		this.setAge(age);
		this.setGrade(grade);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name.length() > 10) {
			throw new IllegalArgumentException("name's length should <=10 " + name.length());
		}
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age <= 0) {
			throw new IllegalArgumentException("age should >0 " + age);
		}
		this.age = age;
	}

	public double getGrade() {
		return grade;
	}

	public void setGrade(double grade) {
		if (grade < 0 || grade > 100) {
			throw new IllegalArgumentException("grade should be in [0,100] " + grade);
		}
		this.grade = grade;
	}

}
